﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    //抽象类
    abstract class Shape
    {
        abstract public double Area();//抽象方法，计算面积
        abstract public bool IsLegal();//抽象方法，判断形状是否合法
    }

    //长方形类，继承Shape类
    class Rectangle : Shape
    {
        private float length;//长方形的长
        private float width;//宽

        public Rectangle(float length, float width)
        {
            if(length == 0 || width == 0)
            {
                Console.WriteLine("数据非法");
                return;
            }
            this.length = length;
            this.width = width;
        }

        public float Length
        {
            get { return length; }
            set { length = value; }
        }

        public float Width
        {
            get { return width; }
            set { width = value; }
        }

        public override double Area()
        {
            return length * width;
        }

        public override bool IsLegal()
        {
            if (length == 0 || width == 0)//长或宽为0是不合法
                return false;
            return true;
        }
    }

    //正方形类，继承Shape类
    class Square: Shape
    {
        private float a;

        public float A
        {
            get { return a; }
            set { a = value; }
        }

        public Square(float a)
        {
            if (a == 0)
            {
                Console.WriteLine("数据非法");
                return;
            }
            this.a = a;
        }
        public override double Area()
        {
            return a * a;
        }

        public override bool IsLegal()
        {
            return !(a == 0);
        }
    }

    //三角形类，继承Shape类
    class Triangle: Shape
    {
        private double x;//三角形的三边
        private double y;
        private double z;

        public double X
        {
            get { return x; }
            set { x = value; }
        }
        public double Y
        {
            get { return y; }
            set { y = value; }
        }
        public double Z
        {
            get { return z; }
            set { z = value; }
        }
        public Triangle(double x, double y, double z)
        {
            if(x + y > z && x + z > y && y + z > x)
            {
                this.x = x;
                this.y = y;
                this.z = z;
            }
            else
            {
                Console.WriteLine("数据非法");
            }
        }
        public override double Area()
        {
            double C=(x + y + z)/2.0;
            return Math.Sqrt(C * (C - x) * (C - y) * (C - z));//海伦公式求三角形的面积 
        }

        public override bool IsLegal()
        {
            if (x + y > z && x + z > y && y + z > x)
                return true;
            return false;
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Shape rectangle = new Rectangle(5f, 6f);
            Shape square = new Square(5f);
            Shape triangle = new Triangle(3d, 4d, 5d);
            Console.WriteLine(rectangle.Area());
            Console.WriteLine(square.Area());
            Console.WriteLine(triangle.Area());
        }
    }
}
